{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f4709570",
   "metadata": {},
   "source": [
    "# 什么是插值法？\n",
    "插值法是一种在给定的点之间生成点的方法。\n",
    "\n",
    "例如：对于点1和2，我们可以通过插值找到点1.33和1.66。\n",
    "\n",
    "插值法有很多用途，在机器学习中，我们经常处理数据集中的缺失数据，内插法经常被用来替代这些值。\n",
    "\n",
    "这种填补数值的方法被称为归因法。\n",
    "\n",
    "除了估算，内插法还经常用于我们需要平滑数据集中的离散点的地方。\n",
    "\n",
    "# 如何在SciPy中实现它？\n",
    "SciPy为我们提供了一个叫scipy.interpolate的模块，它有许多处理插值的函数。\n",
    "\n",
    "# 一维插值\n",
    "函数interp1d()是用来插值一个有1个变量的分布。\n",
    "\n",
    "它接收x和y点并返回一个可调用的函数，可以用新的x来调用并返回相应的y。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cd3eb216",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  3  5  7  9 11 13 15 17 19]\n",
      "[5.2 5.4 5.6 5.8 6.  6.2 6.4 6.6 6.8]\n"
     ]
    }
   ],
   "source": [
    "from scipy.interpolate import interp1d\n",
    "import numpy as np\n",
    "\n",
    "xs = np.arange(10)\n",
    "ys = 2*xs + 1\n",
    "\n",
    "interp_func = interp1d(xs, ys)\n",
    "\n",
    "newarr = interp_func(np.arange(2.1, 3, 0.1))\n",
    "print(ys)\n",
    "print(newarr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f7d8fa3",
   "metadata": {},
   "source": [
    "# 样条插值\n",
    "在一维插值中，点被拟合为一条曲线，而在样条插值中，点被拟合为一个用多项式定义的片状函数，称为样条。\n",
    "\n",
    "UnivariateSpline()函数接收xs和ys并产生一个可调用的函数，可以用新的xs调用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "db1f8d2e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5.62826474 6.03987348 6.47131994 6.92265019 7.3939103  7.88514634\n",
      " 8.39640439 8.92773053 9.47917082]\n"
     ]
    }
   ],
   "source": [
    "from scipy.interpolate import UnivariateSpline\n",
    "import numpy as np\n",
    "\n",
    "xs = np.arange(10)\n",
    "ys = xs**2 + np.sin(xs) + 1\n",
    "\n",
    "interp_func = UnivariateSpline(xs, ys)\n",
    "\n",
    "newarr = interp_func(np.arange(2.1, 3, 0.1))\n",
    "\n",
    "print(newarr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20fe2f1c",
   "metadata": {},
   "source": [
    "# 径向基函数的插值\n",
    "径向基函数是一个对应于固定参考点定义的函数。\n",
    "\n",
    "Rbf()函数也以xs和ys为参数，并产生一个可调用的函数，可以用新的xs调用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c53f225a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6.25748981 6.62190817 7.00310702 7.40121814 7.8161443  8.24773402\n",
      " 8.69590519 9.16070828 9.64233874]\n"
     ]
    }
   ],
   "source": [
    "from scipy.interpolate import Rbf\n",
    "import numpy as np\n",
    "\n",
    "xs = np.arange(10)\n",
    "ys = xs**2 + np.sin(xs) + 1\n",
    "\n",
    "interp_func = Rbf(xs, ys)\n",
    "\n",
    "newarr = interp_func(np.arange(2.1, 3, 0.1))\n",
    "\n",
    "print(newarr)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
